package com.tarena.lbs.user.service.impl;


import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.api.BusinessApi;
import com.tarena.lbs.business.api.pojo.BusinessDto;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminRepository adminRepository;

    @Override
    public AdminVO detail() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        log.info("userPrinciple:{}", userPrinciple);
        //断言异常
        Asserts.isTrue(userPrinciple == null, new BusinessException("-2", "认证解析错误"));
        Integer id = userPrinciple.getId();
        //2.读取数据库po
        AdminPO po = adminRepository.getAdminById(id);
        //3.转化成vo VO就是PO的复制改名 属性几乎一模一样的
        AdminVO vo = null;
        if (po != null) {
            vo = new AdminVO();
            //密码最好设置成空
            BeanUtils.copyProperties(po, vo);
        }
        return vo;
    }
    @Override
    public AdminDto detail(Integer id){
        AdminPO adminPO = adminRepository.getAdminById(id);
        AdminDto dto = null;
        if (adminPO != null){
            dto = new AdminDto();
            BeanUtils.copyProperties(adminPO,dto);
        }
        return dto;
    }
    @Override
    public PageResult<AdminVO> pageList(AdminQuery query) {
        PageResult<AdminVO> voPage = new PageResult<>();
        PageInfo<AdminPO> pageInfo = adminRepository.pagesList(query);
        voPage.setPageNo(pageInfo.getPageNum());
        voPage.setPageSize(pageInfo.getPageSize());
        voPage.setTotal(pageInfo.getTotal());
        List<AdminVO> vos = null;
        List<AdminPO> pos = pageInfo.getList();
        if (!CollectionUtils.isEmpty(pos)){
            vos = pos.stream().map(po->{
                AdminVO adminVO = new AdminVO();
                BeanUtils.copyProperties(po,adminVO);
                return adminVO;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }
    @Override
    public void save(AdminParam param) throws BusinessException {
        log.debug("保存账号业务处理,入参:{}",param);
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        log.info("userPrinciple:{}", userPrinciple);
        Asserts.isTrue(userPrinciple == null, new BusinessException("-2", "认证解析错误"));
        Roles role = userPrinciple.getRole();
        if (role == Roles.ADMIN) {
            if(param.getAccountType() == 1){
                saveShop(param);
            }else if(param.getAccountType() == 0)
            saveAdmin(param);
        } else {
            new BusinessException("-2","权限不足");
        }


    }

    @Override
    public void delete(Integer id) {
        adminRepository.delete(id);
    }

    @Override
    public void update(AdminParam param) {
        AdminPO adminPO = new AdminPO();
        BeanUtils.copyProperties(param,adminPO);
        adminRepository.update(adminPO);
    }

    private void saveAdmin(AdminParam  param) throws BusinessException {
        AdminPO adminPO = new AdminPO();
        BeanUtils.copyProperties(param,adminPO);
        adminPO.setCreateTime(new Date());
        adminPO.setBusinessId(0);
        adminRepository.save(adminPO);
    }

    private void saveShop(AdminParam param) throws BusinessException {
        Integer businessId = param.getBusinessId();
        log.info("businessId:{}",businessId);
        Asserts.isTrue(businessId == null, new BusinessException("-2","商家不存在"));
        AdminPO adminPO = new AdminPO();
        BeanUtils.copyProperties(param,adminPO);
        adminPO.setCreateTime(new Date());
        adminPO.setBusinessId(businessId);
        log.debug("保存商家账号业务处理,商家id:{}",adminPO.getBusinessId());
        adminPO.setAccountType(1);
        log.debug("保存商家账号业务处理,最后入参:{}",adminPO);
        adminRepository.save(adminPO);
    }

}
